#delimit;

****;
version 9.0;

clear;
clear matrix;
estimates clear;
eststo clear;
set more off;
set mem 400m;
set seed 191206;

*All Worksites, panel;

cap prog drop runme;
prog def runme;

local hypothesis = `0';
tempfile main bootsave;

use "$da/Crimes-Against-Morality_Main_data-constructed.dta",clear;
keep if dataset==1;

*2. Setting Macros;
*2.1. Setting sample restrictions;
local s1 = "if sw==1&full==1";
	*still in sex work;
local s2 = "if panel_v3==1";
	*in the panel and still in sex work;

*2.2 setting controls;
local char = 
	"divwid unmarried edyrs age children sw_locyrs disc_factor risktol_nodl nodlr";

di ;

areg $var dd_cl el `char' `s2', cluster(wsid) abs(wsid);
global mainbeta = _b[dd_cl];
global maint = (_b[dd_cl]-`hypothesis')/_se[dd_cl];
predict epshat, resid;
predict yhat, xb;

*also general the "impose the null hypothesis" yhat and residual;
qui gen temp_y = $var - dd_cl*`hypothesis';
areg temp_y el `char' `s2', abs(wsid);
predict epshat_imposed, resid;
predict yhat_imposed, xb;
qui replace yhat_imposed = yhat_imposed + dd_cl*`hypothesis';

sort wsid el closing;
qui save `main', replace;

*getting a count of then number of clusters in the sample;
qui by wsid: keep if _n==1;
qui summ;
global numclust = r(N);

cap erase `bootsave';
qui postfile bskeep beta_np t_np t_wild
	using `bootsave', replace;

forvalues b=1/$bootreps { ;

qui use `main', replace;
qui by wsid: gen tempbs = uniform();
qui by wsid: gen pos = (tempbs[1] <.5);

qui gen wildresid = epshat_imposed *(2*pos - 1);
qui gen wildy = yhat_imposed + wildresid;
qui areg wildy dd_cl el `char' `s2', cluster(wsid) abs(wsid);
local bst_wild =(_b[dd_cl]-`hypothesis')/_se[dd_cl];

*nonparametric bootstrap;
bsample, cluster(wsid) idcluster(newsid);
qui areg $var dd_cl el `char' `s2', cluster(newsid) abs(newsid);
local bsbeta = _b[dd_cl];
local bst = (_b[dd_cl]-$mainbeta)/_se[dd_cl];

post bskeep (`bsbeta') (`bst') (`bst_wild');
};
*;
qui postclose bskeep;

qui drop _all;
qui set obs 1;
qui gen t_np = $maint;
qui gen t_wild = $maint;
qui append using `bootsave';

qui gen n = .;

foreach stat in t_np t_wild {;
qui summ `stat';
local bign = r(N);
sort `stat';
qui replace n = _n;
qui summ n if abs(`stat'-$maint)< 0.000001;
local myp = r(mean) / `bign';
global pctile_`stat' = 2*min(`myp', (1-`myp'));
};
*;

global mainp = norm($maint);
global pctile_main = 2*min($mainp, (1-$mainp));

local myfmt = "%7.5f";

di;
di "Number BS Reps = $bootreps, Null hypothesis = `hypothesis'";
display "Main beta" _column(13) "main T" _column(22) "Main %le" 
		_column(33) "NP BS %le" _column(44) "wild %le";
di %6.3f $mainbeta _column(13) %6.3f $maint _column(23) 
		`myfmt' $pctile_main _column(34) `myfmt' $pctile_t_np 
		_column(44) `myfmt' $pctile_t_wild ;
		
end;

global bootreps = 999;
runme 0;

****************************************;
*Formal Only, panel;

cap prog drop runme;
prog def runme;

local hypothesis = `0';
tempfile main bootsave;

use "$da/Crimes-Against-Morality_Main_data-constructed.dta",clear;
keep if dataset==1;


*2. Setting Macros;
*2.1. Setting sample restrictions;
local s1 = "if sw==1&full==1";
	*still in sex work;
local s2 = "if panel_v3==1";
	*in the panel and still in sex work;

*2.2 setting controls;
local char = 
	"divwid unmarried edyrs age children sw_locyrs disc_factor risktol_nodl nodlr";

di ;

areg $var dd_cl el `char' `s2'&formal==1, cluster(wsid) abs(wsid);
global mainbeta = _b[dd_cl];
global maint = (_b[dd_cl]-`hypothesis')/_se[dd_cl];
predict epshat, resid;
predict yhat, xb;

*also general the "impose the null hypothesis" yhat and residual;
qui gen temp_y = $var - dd_cl*`hypothesis';
areg temp_y el `char' `s2'&formal==1, abs(wsid);
predict epshat_imposed, resid;
predict yhat_imposed, xb;
qui replace yhat_imposed = yhat_imposed + dd_cl*`hypothesis';

sort wsid el closing;
qui save `main', replace;

*getting a count of then number of clusters in the sample;
qui by wsid: keep if _n==1;
qui summ;
global numclust = r(N);

cap erase `bootsave';
qui postfile bskeep beta_np t_np t_wild
	using `bootsave', replace;

forvalues b=1/$bootreps { ;

qui use `main', replace;
qui by wsid: gen tempbs = uniform();
qui by wsid: gen pos = (tempbs[1] <.5);

qui gen wildresid = epshat_imposed *(2*pos - 1);
qui gen wildy = yhat_imposed + wildresid;
qui areg wildy dd_cl el `char' `s2'&formal==1, cluster(wsid) abs(wsid);
local bst_wild =(_b[dd_cl]-`hypothesis')/_se[dd_cl];

*nonparametric bootstrap;
bsample, cluster(wsid) idcluster(newsid);
qui areg $var dd_cl el `char' `s2'&formal==1, cluster(newsid) abs(newsid);
local bsbeta = _b[dd_cl];
local bst = (_b[dd_cl]-$mainbeta)/_se[dd_cl];

post bskeep (`bsbeta') (`bst') (`bst_wild');
};
*;
qui postclose bskeep;

qui drop _all;
qui set obs 1;
qui gen t_np = $maint;
qui gen t_wild = $maint;
qui append using `bootsave';

qui gen n = .;

foreach stat in t_np t_wild {;
qui summ `stat';
local bign = r(N);
sort `stat';
qui replace n = _n;
qui summ n if abs(`stat'-$maint)< 0.000001;
local myp = r(mean) / `bign';
global pctile_`stat' = 2*min(`myp', (1-`myp'));
};
*;

global mainp = norm($maint);
global pctile_main = 2*min($mainp, (1-$mainp));

local myfmt = "%7.5f";

di;
di "Number BS Reps = $bootreps, Null hypothesis = `hypothesis'";
display "Main beta" _column(13) "main T" _column(22) "Main %le" 
		_column(33) "NP BS %le" _column(44) "wild %le";
di %6.3f $mainbeta _column(13) %6.3f $maint _column(23) 
		`myfmt' $pctile_main _column(34) `myfmt' $pctile_t_np 
		_column(44) `myfmt' $pctile_t_wild ;
		
end;

global bootreps = 999;
runme 0;

